/*
 * @Descripttion:
 * @Author: cuidm
 * @Date: 2022-07-07 15:27:12
 * @LastEditors: cuidm
 * @LastEditTime: 2022-11-01 11:29:46
 */
import React, { useState, useEffect, useRef } from "react";
import { Form, Button, Input, Affix, Select, message, Space, Switch, Drawer, Row, Col, InputNumber, Modal } from "antd";
import { MinusCircleOutlined, PlusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import { ModelFormItem } from "@/components";
import cmdbres from "@/data/cmdbres";
import request, {requestIAM} from '@/utils/request';
import regexpUtil from "@/utils/regexpUtil";
import hcpUtils from "@/utils/hcpUtils";

const user = hcpUtils.getUser();
const CreateResource = ({ history, visible, setVisible, onCreateFinish, cmModuleId }) => {
    const modelFormItemRef = useRef();
	const [form] = Form.useForm();
    const [loading, setLoading] = useState(false);

	const onFinish = async (fieldsValue) => {
        modelFormItemRef.current.dealDataFn(fieldsValue);
        console.log(fieldsValue);
        setLoading(true);
		let res = await request({
			...cmdbres.addResource,
			data: {
                moduleId: cmModuleId,
                data: {
                    ...fieldsValue,
                    cm_appkey: "控制台",
                    cm_isdeleted: "N",
                },
                author: user.staffName,
            }

		}).catch(err=>{
            setLoading(false)
        });
		setLoading(false);
		if (res && res.code == 200) {
			setVisible(false);
			message.success(`保存成功！`);
			if (typeof onCreateFinish == "function") onCreateFinish(res);
		}
	};

    const handleClose = () => {
        setVisible(false);
    }

    return (
        <Modal
			title="新建实例"
			className="hcp-modal-md create-field"
            width={600}
			visible={visible}
			footer={null}
			onCancel={handleClose}
			destroyOnClose
			maskClosable={false}
			afterClose={()=>{form.resetFields()}}
		>
            <Form
                form={form}
                labelCol={{ span: 5}}
                wrapperCol={{ span: 17 }}
                name="basic"
                onFinish={onFinish}
            >
                <div style={{height: 300, overflow: 'auto'}}>
                    <ModelFormItem form={form} cRef={modelFormItemRef} cmModuleId={cmModuleId}/>
                </div>
                <div className="hcp-modal-body-footer">
					<Form.Item wrapperCol={{ span: 24 }} >
						<Button onClick={handleClose}>取消</Button>
						<Button type="primary" htmlType="submit" loading={loading}>确认</Button>
					</Form.Item>
				</div>
            </Form>
        </Modal>
    );
};

export default CreateResource;
